package org.spring.springboot.dao;

import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;
import org.spring.springboot.domain.City;
import org.springframework.transaction.annotation.Transactional;

@Mapper
@CacheNamespace(implementation = MybatisRedisCache.class)
public interface CityDao {


	
	@Select("SELECT id,province_id,city_name,description FROM city WHERE id=#{id}")
	@Results(id = "cityResult", value = {
			@Result(property = "id", column = "id"),
			@Result(property = "province", column = "province_id", one = @One(select = "org.spring.springboot.dao.ProvinceDao.findById")),
			@Result(property = "cityName", column = "city_name"),
			@Result(property = "description", column = "description") 
			})
	@Options(useCache = false,statementType=StatementType.PREPARED)
	public City findById(@Param("id") long id);

	@Select("SELECT id,province_id,city_name,description FROM city WHERE city_name=#{cityName}")
	@ResultMap("cityResult")
	@Options(useCache = false,statementType=StatementType.PREPARED)
	public City findByName(@Param("cityName") String cityName);

	@SelectProvider(type = CitySqlProvider.class, method = "find")
	@ResultMap("cityResult")
	@Options(useCache = false)
	public List<City> findMore(@Param("city") City city);

	@Select("SELECT id,province_id,city_name,description FROM city")
	@ResultMap("cityResult")
	public List<City> findAll();

	@InsertProvider(type = CitySqlProvider.class, method = "add")
	@Options(useGeneratedKeys = true, keyProperty = "city.id")
	void addOne(@Param("city") City city);

	@Insert("<script>" + "insert into city (city_name, province_id ,description) values "
			+ "<foreach collection=\"list\" item=\"city\" separator=\",\" >"
			+ "(#{city.cityName}, #{city.province.id}, #{city.description})" + "</foreach>" + "</script>")
	@Options(useGeneratedKeys = true, keyProperty = "id")
	int addCities(List<City> cities);

}
